<?php
/**
 * ali oss
 * Created by PhpStorm.
 * Date: 2018/10/15
 */

namespace common\helpers;

use OSS\Core\OssException;
use OSS\OssClient;

class OssHelper
{
    private static $instance;

    /**
     * @var OssClient
     */
    protected $client;
    protected $bucket;
    protected $accessKeyId;
    protected $accessKeySecret;
    //节点
    protected $endpoint;
    protected $cndHost;

    /**
     * OssHelper constructor.
     * @throws OssException
     */
    private function __construct()
    {
        $ossConf = \Yii::$app->params['oss_conf'];
        $this->accessKeyId = $ossConf['accessKeyId'];
        $this->accessKeySecret = $ossConf['accessKeySecret'];

        $this->endpoint = $ossConf['endpoint'];
        $this->bucket = $ossConf['bucket'];
        $this->cndHost = $ossConf['cndHost'];

        $this->client = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
    }

    private function __clone()
    {
    }

    /**
     * @param null $bucket
     * @return |null
     * @throws OssException
     */
    private static function getEnvBucketName($bucket = null)
    {
        return $bucket ? $bucket : self::getInstance()->bucket;
    }

    /**
     * @return OssHelper
     * @throws OssException
     */
    public static function getInstance()
    {
        if (self::$instance instanceof OssHelper) {
            return self::$instance;
        }
        self::$instance = new OssHelper();
        return self::$instance;
    }

    /**
     * 上传字符串
     * @param string $object
     * @param string $content
     * @param string $bucket
     * @return bool
     * @throws OssException
     */
    public static function putObject($object, $content, $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        self::getInstance()->client->putObject($bucket, $object, $content);
        return true;
    }

    /**
     * 上传文件
     * @param string $object
     * @param string $localFile
     * @param string $bucket
     * @return bool;
     * @throws OssException
     */
    public static function uploadFile($object, $localFile, $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        self::getInstance()->client->uploadFile($bucket, $object, $localFile);
        return true;
    }

    /**
     * 下载到文件或内存
     * @param string $object
     * @param string $localFile
     * @param string $bucket
     * @return mixed
     * @throws OssException
     */
    public static function getObject($object, $localFile = '', $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        if (empty($localFile)) {
            return self::getInstance()->client->getObject($bucket, $object);
        }
        $options = [
            OssClient::OSS_FILE_DOWNLOAD => $localFile
        ];
        self::getInstance()->client->getObject($bucket, $object, $options);
        return true;
    }

    /**
     * 判断文件是否存在
     * @param string $object
     * @param string $bucket
     * @return bool
     * @throws OssException
     */
    public static function objectExists($object, $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        return self::getInstance()->client->doesObjectExist($bucket, $object);
    }

    /**
     * 删除文件
     * @param $object
     * @param string $bucket
     * @return null
     * @throws OssException
     */
    public static function deleteObject($object, $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        return self::getInstance()->client->deleteObject($bucket, $object);
    }


    /**
     * 删除多文件
     * @param array $objects
     * @param null $bucket
     * @return \OSS\Http\ResponseCore
     * @throws OssException
     */
    public static function deleteObjects(array $objects, $bucket = null)
    {
        $bucket = self::getEnvBucketName($bucket);
        self::getInstance()->bucket = $bucket;
        return self::getInstance()->client->deleteObjects($bucket, $objects);
    }


    /**
     * 如果Object是公共读/公共读写权限，那么访问URL的格式为：Bucket名称.Endpoint/Object名称 （Ps OSS官方文档）
     * @param $object
     * @param $bucket
     * @return string
     * @throws OssException
     */
    public static function getPublicUrl($object, $bucket = null)
    {
        return self::getInstance()->cndHost . '/' . $object;

//        if (YII_ENV_DEV || YII_ENV_TEST) {
//            //测试和开发环境不使用cdn
//            $bucket = self::getEnvBucketName($bucket);
//            $self = self::getInstance();
//            return 'https://' . $bucket . '.' . $self->endpoint . '/' . $object;
//        } else {
//
//        }
    }


}